import config from 'config'
import httpFetch from 'share/httpFetch'

export default {

  processSearchParams(searchParams) {
    let params = {};
    for (let searchName in searchParams) {
      if (searchName === 'formOIDs' || searchName === 'nodeOIDs' ) {
        let tempOIDs = [];
        searchParams[searchName] && searchParams[searchName].length > 0 && searchParams[searchName].map(oid => {
          tempOIDs.push(...oid.split(','));
        });
        params[searchName] = tempOIDs.join(',');
      } else if (searchName !== 'selectedRowKeys' && searchName !== 'selectedRows' && searchName !== 'nodeOIDsOption' &&
        searchName !== 'approvedCheckboxList' && searchName.indexOf('Lable') === -1 && searchName !== 'approvePendingCheckboxList') {
        params[searchName] = searchParams[searchName]
      }
    }
    return params;
  },

  //获取单据列表，finished：true（已审批）、false（待审批），entityType：1001（申请单）、1002（报销单）
  getDocumentType(params) {
    //?entityType=1001&finished=${finished}
    return httpFetch.get(`${config.baseUrl}/api/approvals/custom/form`, params)
  },
  getDocumentTypev2(params) {
    //?entityType=1001&finished=${finished}
    return httpFetch.get(`${config.baseUrl}/api/approvals/custom/form/v2`, params)
  },
  //获取审批列表，finished：true（已审批）、false（待审批
  getApproveList(finished, entityType, page, size, searchParams) {
    let url = `${config.baseUrl}/api/approvals/batchfilters/v4`;
    let params = this.processSearchParams(searchParams);
    params.entityType = entityType === 1111 ? '' : entityType;
    params.finished = finished;
    params.page = page;
    params.size = size;
    params.sort = 'submittedDate,DESC';
    return httpFetch.get(url, params)
  },
  //获取待提交列表
  getSubmittingList(entityType, page, size, searchParams) {
    let url = `${config.baseUrl}/api/approvals/batchfilters/tosubmit`;
    let params = this.processSearchParams(searchParams);
    
    params.entityType = entityType === 1111 ? '' : entityType;
    //params.finished = finished;
    params.page = page;
    params.size = size;
    params.sort = 'submittedDate,DESC';
    return httpFetch.get(url, params)
  },

  //根据审批节点oid批量查节点名称
  getApproveNodeName(params) {
    return httpFetch.post(`${config.baseUrl}/brms/api/rule/approval/batch/node/name`, params)
  },

  //查看未来节点
  getFutureNode(params) {
    return httpFetch.post(`${config.baseUrl}/api/approvals/next/all/ruleApprovalNodeOID`, params)
  },

  //单据暂挂
  handleEntityHangUp(params) {
    return httpFetch.post(`${config.baseUrl}/api/approvals/pending`, params)
  },

  //处理全量的审批，通过或驳回
  handleAllApprove(entityType, searchParams, body) {
    let url = `${config.baseUrl}/api/approvals/batch/operate`;
    let params = this.processSearchParams(searchParams);
    params.entityType = entityType === 1111 ? '' : entityType;
    let first = true;
    for (let key in params) {
      if (params[key]) {
        if (first) {
          url += `?${key}=${params[key]}`;
          first = false;
        } else {
          url += `&${key}=${params[key]}`;
        }
      }
    }
    return httpFetch.post(url, body)
  },

  
  //审批通过
  handleRequestApprovePass(params) {
    return httpFetch.post(`${config.baseUrl}/api/approvals/pass`, params)
  },

  //审批驳回
  handleRequestApproveReject(params) {
    return httpFetch.post(`${config.baseUrl}/api/approvals/reject`, params)
  },

  //提交
  handleRequestSub(params) {
    return httpFetch.post(`${config.baseUrl}/api/documents/tosubmit/batch`, params)
  },

  
  //是否可以加签 counterSignType：enableAddSignForSubmitter（验证提交人是否可加签，单据为编辑状态）、enableAddSign（验证审批人审批单据时是否可加签）
  isCounterSignEnable(companyOID, formOID, counterSignType) {
    let params = {
      companyOID, formOID, counterSignType
    };
    return httpFetch.get(`${config.baseUrl}/api/countersign/addSign/enable`, params)
  },

  //加签人的选择范围人员
  postAddSignEnableScope(params){
    return httpFetch.post(`${config.baseUrl}/api/countersign/addSign/enable/and/scope`, params)
  },

  getUserAvatar(userOID){
    return httpFetch.get(`${config.baseUrl}/api/users/oids?page=0&size=1&userOIDs=${userOID}`)
  },

  //提交审批转交
  postApproveTransfer(params){
    return httpFetch.post(`${config.baseUrl}/api/approvals/transmit`, params)
  },

  //向前加签
  postForwardAdditional(params) {
    return httpFetch.post(`${config.baseUrl}/api/approvals/countersign`, params)
  }
}
